let express = require('express')
let fs = require('fs')
let Student = require('./modules/student')
let Msg = require('./modules/msg')
let History = require('./modules/history')
let User = require('./modules/user')
let router = express.Router()


//处理信息start
//学生数据数据接口
router.get('/checklogin', (req, res) => {
    res.send(req.session.nick)
})

router.get('/students', (req, res) => {
    Student.find((err, students) => {
        if (err) {
            return res.status(500).send('Server error.')
        }
        var data = students
        var user = req.session.user
        res.send([data, user])

    })
})



//处理添加学生接口
router.post('/students/new', function (req, res) {
    Student.save(req.body, function (err, err_code) {
        var code = {}
        code.err_code = err_code
        if (err) {
            return res.status(500).send('Server error.')
        }
        res.send(code);
    })
})

router.get('/students/edit', function (req, res) {
    Student.findById(parseInt(req.query.id), function (err, student) {
        if (err) {
            return res.status(500).send('Server error.')
        }
        res.send(student)
    })

})

//处理编辑请求接口
router.post('/students/edit', function (req, res) {
    Student.updateById(req.body, function (err, err_code) {
        if (err) {
            return res.status(500).send('Server error.')
        }
        var code = {}
        code.err_code = err_code
        res.send(code);
    })
})

//处理删除学生信息接口
router.get('/students/delete', function (req, res) {
    Student.deleteById(req.query.id, function (err) {

        if (err) {
            return res.status(500).send('Server error.')
        }
        res.send()
    })
})

//获取编辑页面数据接口
router.get('/msg/edit', function (req, res) {
    Msg.findById(parseInt(req.query.id), function (err, info) {
        if (err) {
            return res.status(500).send('Server error.')
        }
        res.send(info)
    })

})
//处理信息end

//处理留言信息start
router.get('/msg', function (req, res) {
    Msg.find(function (err, infos) {
        if (err) {
            return res.status(500).send('Server error.')
        }
        var data = infos
        res.send(data)
    })
})
//处理添加请求
router.post('/msg/new', function (req, res) {
    Msg.save(req.body, (err, err_code) => {
        var data = { "code": err_code }
        if (err) return res.status(500).send("Server error")
        res.send(data)
    })
})

//渲染留言板编辑页面
router.get('/msg/edit', function (req, res) {
    Msg.findById(parseInt(req.query.id), function (err, info) {
        if (err) {
            return res.status(500).send('Server error.')
        }
        res.send(info)
    })

})


//处理编辑请求
router.post('/msg/edit', function (req, res) {
    Msg.updateById(req.body, function (err, err_code) {
        var code = { "code": err_code }
        if (err) {
            return res.status(500).send('Server error.')
        }
        res.send(code)
    })
})


router.get('/msg/delete', function (req, res) {
    Msg.deleteById(req.query.id, function (err) {
        if (err) {
            return res.status(500).send('Server error')
        }
        res.send()
    })
    
})
//处理留言信息end


//处理回收站start
router.get('/history', function (req, res) {
    History.find(function (err, historys) {
        if (err) {
            return res.status(500).send('Server error.')
        }
        var data = historys
        res.send(data)
    })
})

router.post('/history/new', function (req, res) {
    History.save(req.body, function (err, err_code) {
        var code = { "code": err_code }
        if (err) {
            return res.status(500).send('Server error.')
        }
        res.send(code);
    })
})

router.get('/history/delete', function (req, res) {
    
    History.deleteById(req.query.id, function (err) {
        if (err) {
            return res.status(500).send('Server error')
        }
        res.send()

    })

})


router.post('/register', function (req, res) {
    User.register(req.body, function (err, err_code) {
        var code = { "code": err_code }
        if (err) throw err
        res.send(code);

    })
})


router.post('/login', function (req, res) {
    User.login(req.body, function (err, err_code, nick) {
        if (err) throw err
        req.session.user = nick
        var code = { "code": err_code, "session": req.session.user }
        res.send(code)
    })
})

router.get('/logout', function (req, res) {
    delete req.session.user
    res.end();
})


module.exports = router
